package com.javabi.codegenerator.java.object.datastore.method.sql;

import com.javabi.codegenerator.java.model.bean.JavaModelBeanField;
import com.javabi.codegenerator.java.object.datastore.method.ContainsColumnMethod;
import com.javabi.codetype.sql.builder.ISqlBuilder;
import com.javabi.codetype.sql.executor.ISqlValues;
import com.javabi.codetype.sql.executor.SqlValues;

public class SqlContainsColumnMethod extends ContainsColumnMethod {

	public SqlContainsColumnMethod(JavaModelBeanField field) {
		super(field);
		setOverride();

		getReferences().add(ISqlBuilder.class);
		getReferences().add(ISqlValues.class);
		getReferences().add(SqlValues.class);

		setSqlContents(field);
	}

	private void setSqlContents(JavaModelBeanField field) {
		String name = field.getName();

		SqlCodeLines code = new SqlCodeLines();
		code.emptyLine();
		code.line("// SQL");
		code.line("ISqlBuilder sql = newSqlBuilder();");
		code.line("sql.selectCountAllFrom(getTable());");

		// Where
		code.line("sql.where();");
		code.line("sql.column(\"" + name + "\").sql(\"=\").value(" + name + ");");

		// Execute
		code.emptyLine();
		code.line("// Execute");
		code.line("return getExecutor().getCount(sql) > 0;");
		setContents(code);
	}
}
